package com.jule.robot.strategy.impl;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.robot.model.PlayerInfo;
import com.jule.robot.service.holder.FunctionIdHolder;
import com.jule.robot.service.holder.RobotMoneyPoolHolder;
import com.jule.robot.service.websocket.BaseWebSocketClient;
import com.jule.robot.strategy.BaseRobotStrategry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StandUpStrategry extends BaseRobotStrategry {
    private final static Logger logger = LoggerFactory.getLogger(StandUpStrategry.class);
    public StandUpStrategry() {
    }

    @Override
    public void doAction(BaseWebSocketClient client, int functionId, byte[] bytes) {
//        setClient(client);
        try {
            JoloGame.JoloGame_ApplyStandUpAck ack = JoloGame.JoloGame_ApplyStandUpAck.parseFrom(bytes);
            logger.debug("ACK Header, functionId->"+functionId+", functionName->"+ FunctionIdHolder.GetFunctionName(functionId)+", ACK Body, result->" + ack.getResult() + ", ResultMsg->" + ack.getResultMsg()
                    + ", userId->"+ack.getUserId()+", roomId->"+ack.getRoomId() +", tableId->"+ack.getTableId());
            if(ack.getResult() == 1 || ack.getResult() == -2 || ack.getResult() == -1){
                if(ack.getResult() == -1){ //-1 表示game的牌桌信息中找不到此用户，说明机器人已离桌
                    logger.info("站起失败|Game找不到用户信息，机器人默认为站起成功， userId->{}, roomId->{}, tableId->{}, ackResult->{}, resultMsg->{}",
                            ack.getUserId(), ack.getRoomId(), ack.getTableId(), ack.getResult(), ack.getResultMsg());
                }
                ExecuteLeaveTable(client, ack.getCurrStoreScore()); //离开桌子
            }else{
                logger.error("站起失败， userId->{}, roomId->{}, tableId->{}, ackResult->{}, resultMsg->{}", ack.getUserId(), ack.getRoomId(), ack.getTableId(), ack.getResult(), ack.getResultMsg());
            }
        }catch (Exception ex){
            logger.error("AckStandUp error, msg = "+ex.getMessage(), ex);
        }
    }

    //离桌
    public void ExecuteLeaveTable(BaseWebSocketClient client, double playScoreStore){
        JoloGame.JoloGame_ApplyLeaveReq.Builder req = JoloGame.JoloGame_ApplyLeaveReq.newBuilder();
        req.setUserId(client.getUserId());
        req.setRoomId(client.getRoomId());
        req.setTableId(client.getTableId());

        int reqNum = (int)(System.currentTimeMillis()/1000);
        int functionId = FunctionIdHolder.Game_REQ_ApplyLeave;

        logger.debug("申请离开桌子 Body, reqNum->"+reqNum+", functionName->"+FunctionIdHolder.GetFunctionName(functionId)+", userId->" + req.getUserId() + ", tableId->" + req.getTableId()
                + ", roomId->"+req.getRoomId());

        client.sendData(functionId, client.getGameId(), reqNum, req.build().toByteArray(), client.getGameSvrId());
    }
}
